package com.alex.server.service.impl;

import com.alex.server.mapper.MenuRoleMapper;
import com.alex.server.pojo.MenuRole;
import com.alex.server.pojo.RespBean;
import com.alex.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MenuRoleService extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {
    @Autowired
    private  MenuRoleMapper menuRoleMapper;
    @Override
    @Transactional
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        //根据角色id删除,与其相关的所有权限,然后在将所需求的权限,全部加上
        menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid",rid));
        //这里的意思为,就是判断前端传来的数组中,是否含有权限,如果设置这个角色没有任何权限,就直接返回更新成功
        if (mids==null||0==mids.length){
            return RespBean.success("更新成功");
        }
        //如果权限不为空,则我们需要把这些权限给这个角色加进去
        Integer result = menuRoleMapper.insertRecord(rid, mids);
        if (result==mids.length) {
            return RespBean.success("更新成功");
        }else {
            return RespBean.error("更新失败");
        }
    }
}
